package java8;

import java.util.ArrayList;
import java.util.List;

public class ColDim {
    public static void main(String[] args) {
        List<List<String>> records = new ArrayList<>();
        List<String> record1 = new ArrayList<>();
        record1.add("a1");
        record1.add("a2");
        record1.add("a3");

        List<String> record2 = new ArrayList<>();
        record2.add("b1");
        record2.add("b2");
        record2.add("b3");
        List<String> record3 = new ArrayList<>();
        record3.add("c1");
        record3.add("c2");
        record3.add("c3");
        List<String> record4 = new ArrayList<>();
        record4.add("a1");
        record4.add("a2");
        record4.add("b1");

        List<String> m = new ArrayList<>();
        m.add("m1");
        m.add("m2");
        int i = 1;

        records.add(record1);
        records.add(record4);
        records.add(record2);
        records.add(record3);

        List<List<String>> newRecords = new ArrayList<>();

        int metricsIndex = 0;

        for (int j = 0; j < records.size(); j++) {
            List<String> newRecord = new ArrayList<>();
            List<String> theItem = records.get(j);
            int l = 0;
            int count = 0;
            while (l < theItem.size() && count < m.size() * theItem.size()) {
                count++;
                if (i == l) {
                    newRecord.add(m.get(metricsIndex));
                    metricsIndex = metricsIndex + 1;
                }

                newRecord.add(theItem.get(l));

                if (i == theItem.size() && l == theItem.size() - 1) {
                    newRecord.add(m.get(metricsIndex));
                    metricsIndex = metricsIndex + 1;
                }

                System.out.println("l:" + l);
                System.out.println("m:" + metricsIndex);

                l++;

                if (metricsIndex == m.size() && l == theItem.size()) {
                    metricsIndex = 0;
                }

                if (l == theItem.size()) {
                    l = 0;
                    newRecords.add(newRecord);
                    newRecord = new ArrayList<>();

                }
                System.out.println("****************");
            }


        }

        printList(records);
        System.out.println("****************");
        printList(newRecords);


    }


    public static void printList(List<List<String>> records) {
        for (int i = 0; i < records.size(); i++) {
            for (int j = 0; j < records.get(i).size(); j++) {
                System.out.print(records.get(i).get(j));
            }
            System.out.println("\n");
        }
    }
}